--
-- Author: shunguo.chen
-- Date: 2017-09-11 11:32:35
--
-- ZoneView	服务器列表

local ScrollViewExtend = require("app.widget.ScrollViewExtend")
local ZoneListRender = import(".ZoneListRender")
local ZoneRender = import(".ZoneRender")
local FriendZoneListRender = import(".FriendZoneListRender")
local ListViewVertical = require("app.widget.listview.ListViewVertical")
local fileName = string.getModeName(...)
local ClassRef = class(fileName, app.mvc.ViewBase)

ClassRef.VIEW_ROOT_RES = Res.LoginZoneScene

function ClassRef:init()
	self._ui.Button_back:addClickEventListener(function()
		executeFunc(self._onOperate, "exit")
	end)

	self._curListRender = nil
	self._list = self._ui.ListView
	self._list.createRender = function (list, idx)
		local items = list:getItems()
    	local render = items[idx]
    	if nil == render then
			render = ZoneListRender.create()
			list:pushBackCustomItem(render)
		end
		render:setVisible(true)
		return render
	end

	self._curZoneRender = nil
	self._sv = ScrollViewExtend.create(self._ui.ScrollView, {class=ZoneRender})


	local params = {
		renderClass = FriendZoneListRender,
		-- paddingLeft = 5,
		-- paddingRight = 5,
		-- gapY = 4,
		cellEventProxy = function(eventName, serverInfo)
			executeFunc(self._onOperate, "selectZone", serverInfo)
		end,
	}
	self._friendListView = ListViewVertical.new( self._ui["ScrollView_qq"], params )
	self._friendListView:setVisible(false)
end

function ClassRef:show()
	self.super.show(self)
	self:_setCurListRender()
end

function ClassRef:_setCurListRender(render)
	if self._curListRender then
		self._curListRender:setSelected(false)
	end
	self._curListRender = render
	if self._curListRender then
		self._curListRender:setSelected(true)
	end
end

function ClassRef:refreshList(datas)
	local selectName
	if self._curListRender then
		selectName = self._curListRender:getName()
	end

	local curSelectIndex = 1
	-- 切换账号的时候 会连接不停的区服， 服务器数目不一样  会导致显示bug
	-- 先不考虑复用 调用次数比较少
	self._list:removeAllChildren()
	self._curListRender = nil
	for i,data in ipairs(datas) do
		local render = self._list:createRender(i)
		render:setData(data)
		render:onBtnClick(function(render)
			local data = render:getData()
			self:_setCurListRender(render)
			executeFunc(self._onOperate, "selectList", data)
		end)
		if selectName == data.name then
			curSelectIndex = i
		end
	end

	if datas[curSelectIndex] then
		local render = self._list:getItem(curSelectIndex -1)
		self:_setCurListRender(render)
		executeFunc(self._onOperate, "selectList", datas[curSelectIndex])
	end
end

function ClassRef:refreshZones(datas)
	if datas then
		if datas.isFriend then
			self._sv:setVisible(false)
			self._friendListView:setVisible(true)
			self._friendListView:reloadData(datas.data)
		else
			self._sv:setVisible(true)
			self._friendListView:setVisible(false)
			self._sv:showDataList(datas, 0, function(render)
				local data = render:getData()
				executeFunc(self._onOperate, "selectZone", data)
			end)
		end
	end
	
end

function ClassRef:remove()
	self._sv:remove()
end

return ClassRef